//
//  HomeStrictlyStoreCell.swift
//  BA_MARRY_SWIFT
//
//  Created by 王洋 on 2021/6/7.
//

import UIKit

class HomeStrictlyStoreCell: UITableViewCell {
    
    var itemModel:HomeADItemModel?

    public static func calculateHeight() -> CGFloat{
        return ((UIScreen.main.bounds.size.width - 20)/3)*1.155 + 5;
    }
    
    lazy var collectionview : UICollectionView = {
        
        var width:Float = Float(UIScreen.main.bounds.size.width - 20)
        var cellWidth:Float = (width/3)-4

        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 5
        layout.itemSize = CGSize(width: CGFloat(cellWidth), height: CGFloat(cellWidth)*1.185);

        
        let collectionview = UICollectionView.init(
            frame: CGRect.zero, collectionViewLayout: layout)
        collectionview.delegate = self
        collectionview.dataSource = self
        collectionview.backgroundColor = UIColor.white
        collectionview.register(UINib.init(nibName: "StrictlyStoreCollectionCell", bundle: nil), forCellWithReuseIdentifier: "StrictlyStoreCollectionCellID")

        return collectionview
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = UIColor.white
        
        self.addSubview(self.collectionview)
        self.collectionview.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.bottom.equalTo(0)
            make.top.equalTo(0)
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func fillCellWithModel(model:HomeADItemModel)  {
        itemModel = model
        
        self.collectionview.reloadData()
        
    }
}

extension HomeStrictlyStoreCell:UICollectionViewDelegate,UICollectionViewDataSource {
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemModel?.sysAdvertisement?.sysAdvEventList.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:StrictlyStoreCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StrictlyStoreCollectionCellID", for: indexPath) as! StrictlyStoreCollectionCell
        let model:HomeADItemEventModel = (itemModel?.sysAdvertisement?.sysAdvEventList[indexPath.row])!
        let url = URL(string: model.url ?? "")
        cell.itemMainImageView.kf.setImage(with: url)
        return cell;
    }
}
